//
//  StickerCollectionViewCell.m
//  Caption
//
//  Created by meishe01 on 2017/8/23.
//  Copyright © 2017年 NewAuto video team. All rights reserved.
//

#import "NvFilterCell.h"
#import "NVHeader.h"
#import "UIImageView+WebCache.h"

@interface NvFilterCell () {
    UIImageView *_coverImage;
    UILabel *_name;
    NvFilterItem *_item;
    UIView *_maskView;
    UIImageView *_selectImageView;
}

@end

@implementation NvFilterCell

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        _coverImage = [[UIImageView alloc] initWithFrame:CGRectMake(7 * SCREANSCALE, 0, frame.size.width - 14* SCREANSCALE, frame.size.width - 14* SCREANSCALE)];
        _coverImage.contentMode = UIViewContentModeScaleAspectFit;
        _name = [[UILabel alloc] initWithFrame:CGRectMake(0, frame.size.width - 5 * SCREANSCALE, frame.size.width, frame.size.height-frame.size.width)];
        _name.textAlignment = NSTextAlignmentCenter;
        _name.textColor = UIColor.whiteColor;
        _name.font = [NvUtils fontWithSize:11];
        [self addSubview:_coverImage];
        [self addSubview:_name];
        _coverImage.layer.masksToBounds = YES;
        _coverImage.layer.cornerRadius = 4 * SCREANSCALE;
        
        _maskView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, frame.size.width - 14* SCREANSCALE, frame.size.width - 14* SCREANSCALE)];
        _maskView.frame = _coverImage.frame;
        _maskView.hidden = YES;
        _maskView.backgroundColor = [[UIColor nv_colorWithHexRGB:@"#52D3FF"] colorWithAlphaComponent:.7];
        _maskView.layer.cornerRadius = 4 * SCREANSCALE;
//        _maskView.layer.cornerRadius = ((frame.size.width - 14)/2)* SCREANSCALE;
        [self addSubview:_maskView];
        
        _selectImageView = UIImageView.new;
        _selectImageView.backgroundColor = [UIColor nv_colorWithHexARGB:@"#994A90E2"];
        _selectImageView.contentMode = UIViewContentModeScaleAspectFit;
        [_maskView addSubview:_selectImageView];
        [_selectImageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.center.mas_equalTo(self->_maskView);
        }];
    }
    return self;
}

- (void)renderCellWithItem:(NvFilterItem *)item {
    if (item.isParticle) {
        _selectImageView.image = [NvUtils imageWithName:@"NvParticleItemLayer"];
        _selectImageView.backgroundColor = UIColor.clearColor;
        [_selectImageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.center.mas_equalTo(self->_maskView);
            make.width.offset(26 * SCREANSCALE);
            make.height.offset(26 * SCREANSCALE);
        }];
    }
    _coverImage.frame = CGRectMake(7 * SCREANSCALE, 0, self.frame.size.width - 14* SCREANSCALE, self.frame.size.width - 14* SCREANSCALE);
    _coverImage.layer.masksToBounds = YES;
    _item = item;
    if (item.package) {
        if ([item.cover hasPrefix:@"http"]) {
            [_coverImage sd_setImageWithURL:[NSURL URLWithString:item.cover]];
        }else{
            _coverImage.image = [NvUtils imageWithName:item.cover];
        }
        
    } else {
        _coverImage.image = [NvUtils imageWithName:item.cover];
    }
    _name.text = item.displayName;
    [_name setAlpha:(item.selected ? 1 : 0.6)];
    
    if (item.selected) {
        _selectImageView.hidden = NO;
        _name.textColor = [UIColor nv_colorWithHexRGB:@"#52D3FF"];
    } else {
        _selectImageView.hidden = YES;
        _name.textColor = UIColor.whiteColor;
    }
    if (item.selected || item.longPressed) {
        _maskView.hidden = NO;
    } else {
        _maskView.hidden = YES;
    }
    
    if ([item.cover isEqualToString:@"NvsFilterNone"]) {
        _coverImage.frame = CGRectMake(16.5 * SCREANSCALE, 10 * SCREANSCALE, 25* SCREANSCALE, 25* SCREANSCALE);
        _coverImage.layer.masksToBounds = NO;
    }
}

@end
